package com.jlboot.core.util;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jlboot.core.consts.Consts;
import com.jlboot.model.po.sys.SysIdGenerator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wangsanfei
 * @version 1.0
 * Create by 2024/1/8 14:26
 */
public class ModelUtil {

    public  static <T> void setAddCommon(T t,String table){
        Date now=new Date();
        if(t instanceof Model<?>){
            Model o=(Model) t;
            o.set("id", SysIdGenerator.dao.getNextId(table));
            o.set("del_tag", Consts.COMMON_NONE_DEL);
            o.set("create_time",now);
            o.set("update_time",now);
            o.set("create_by", LoginUtil.getUserId());
            o.set("update_by",LoginUtil.getUserId());
        }else if(t instanceof Record){
            Record o=(Record) t;
            o.set("del_tag", Consts.COMMON_NONE_DEL);
            o.set("create_time",now);
            o.set("update_time",now);
            o.set("create_by", LoginUtil.getUserId());
            o.set("update_by",LoginUtil.getUserId());
        }else{
            throw new RuntimeException("不是Model或Record类型，不能设置通用参数");
        }

    }

    public static <T> void setUpdateCommon(T t){
        Date now=new Date();
        if(t instanceof Model<?>){
            Model o=(Model) t;
            o.set("update_time",now);
            o.set("update_by",LoginUtil.getUserId());
        }else if(t instanceof Record){
            Record o=(Record) t;
            o.set("update_time",now);
            o.set("update_by",LoginUtil.getUserId());
        }else{
            throw new RuntimeException("不是Model或Record类型，不能设置通用参数");
        }
    }

    public static String createInSql(String column,String[] values){
        StringBuilder stringBuilder=new StringBuilder(" "+column+" in (");
        for (int i = 0; i < values.length; i++) {
            if(i>0){
                stringBuilder.append(",");
            }
            stringBuilder.append("?");
        }
        stringBuilder.append(") ");
        return stringBuilder.toString();
    }

}
